package com.yunxi.controller.sto;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.sto.pojo.*;
import com.yunxi.sto.service.StoStockOutPlanDubboService;
import io.swagger.annotations.*;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 车辆出库计划单 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-05-20
 */
@RestController
@RequestMapping("/admin/stoStockOutPlan")
@Api(tags = "sto:stockOutPlan", description = "车辆出库计划单接口")
public class StoStockOutPlanController {
 private static final Logger logger = LoggerFactory.getLogger(StoStockOutPlanController.class);

 @Autowired
 StoStockOutPlanDubboService stoStockOutPlanDubboService;

 @GetMapping("/page")
 @ApiOperation(value = "查询分页")
 public ApiResult page(StoStockOutPlanVO stoStockOutPlanVO, @RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize) throws Exception {
  return ResponseUtil.ok(stoStockOutPlanDubboService.findPageByVO(stoStockOutPlanVO, pageNum, pageSize));
 }

 @GetMapping("/load")
 @ApiOperation(value = "读取单个")
 @ApiImplicitParam(paramType="query", name = "stockOutPlanId", value = "主键id", required = true, dataType = "String")
 public ApiResult load(String stockOutPlanId, @CurrentUser LoginInfo loginInfo) throws Exception {
//  return ResponseUtil.ok(stoStockOutPlanDubboService.getById(stockOutPlanId));
  return ResponseUtil.ok(stoStockOutPlanDubboService.getDetail(stockOutPlanId));
 }

 @PostMapping("/save")
 @ApiOperation(value = "保存信息")
 public ApiResult save(@RequestBody StoStockOutPlanVO stoStockOutPlanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
  stoStockOutPlanDubboService.saveOrUpdate(stoStockOutPlanVO);
  return ResponseUtil.ok("success");
 }

 @PostMapping("/del")
 @ApiOperation(value = "作废")
 public ApiResult del(@RequestBody @ApiParam(value = "主键id集合") List<String> list, @CurrentUser LoginInfo loginInfo) throws Exception {
  stoStockOutPlanDubboService.delAndReset(list,1,loginInfo.getUserCode());
  return ResponseUtil.ok("success");
 }

 @PostMapping("/reset")
 @ApiOperation(value = "恢复")
 public ApiResult reset(@RequestBody @ApiParam(value = "主键id集合") List<String> list, @CurrentUser LoginInfo loginInfo) throws Exception {
  stoStockOutPlanDubboService.delAndReset(list,0,loginInfo.getUserCode());
  return ResponseUtil.ok("success");
 }

 @PostMapping("/print")
 @ApiOperation(value = "打印")
 @ApiImplicitParam(paramType="body", name = "stoStockOutPlanId", value = "主键id", required = true, dataType = "String")
 public ApiResult print(String stoStockOutPlanId) throws Exception {
  stoStockOutPlanDubboService.print(stoStockOutPlanId);
  return ResponseUtil.ok("success");
 }

 @PostMapping("/replace")
 @ApiOperation(value = "换车")
 public ApiResult replace(@RequestBody StoStockOutPlanReplaceVO vo, @CurrentUser LoginInfo loginInfo) throws Exception {
  stoStockOutPlanDubboService.replace(vo,loginInfo.getUserCode());
  return ResponseUtil.ok("success");
 }

 @PostMapping("/setDriver")
 @ApiOperation(value = "分配司机")
 public ApiResult setDriver(@RequestBody StoStockOutDriverVO stoStockOutDriverVO, @CurrentUser LoginInfo loginInfo) throws Exception {
  stoStockOutPlanDubboService.setDriver(stoStockOutDriverVO,loginInfo.getUserCode());
  return ResponseUtil.ok("success");
 }

 @GetMapping("/getDriver")
 @ApiOperation(value = "获取今日上班司机")
 @ApiImplicitParam(paramType="query", name = "planInOutDate", value = "计划出入库日期", required = false, dataType = "String")
 public ApiResult getDriver(String planInOutDate) throws Exception {
  return ResponseUtil.ok(stoStockOutPlanDubboService.getDriver(planInOutDate));
 }

 @GetMapping("/getOutPlanByRfid")
 @ApiOperation(value = "根据RFID获取出库计划")
 @ApiImplicitParam(paramType="query", name = "rfid", value = "司机RFID", required = true, dataType = "String")
 public ApiResult getOutPlanByRfid(String rfid) throws Exception {
  List<StoStockOutPlanVO> list = stoStockOutPlanDubboService.getOutPlanByRfid(rfid);
  return ResponseUtil.ok(list);
 }

 @GetMapping("/vinList")
 @ApiOperation(value = "获取VIN码集合")
 @ApiImplicitParams({
         @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
         @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
 })
 public ApiResult vinList(@CurrentUser LoginInfo loginInfo,Integer pageNum ,Integer pageSize) throws Exception {
  Object partner = loginInfo.getPartner();
  PartnerVO partnerVO = new PartnerVO();
  if(partner != null) BeanUtils.copyProperties(partnerVO,partner);
  return ResponseUtil.ok(stoStockOutPlanDubboService.vinList(partnerVO,pageNum,pageSize));
 }

 @PostMapping("/complete")
 @ApiOperation(value = "完成出库")
 public ApiResult complete(@RequestBody StoStockScanVO stoStockScanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
  stoStockOutPlanDubboService.complete(stoStockScanVO,loginInfo);
  return ResponseUtil.ok("success");
 }

 @PostMapping("/beginStockOut")
 @ApiOperation(value = "开始出库")
 public ApiResult beginStockOut(@RequestBody StoStockScanVO stoStockScanVO) throws Exception {
  stoStockOutPlanDubboService.beginStockOut(stoStockScanVO);
  return ResponseUtil.ok("success");
 }

 @GetMapping("/export")
 @ApiOperation(value = "导出")
 public ApiResult export(StoStockOutPlanVO stoStockOutPlanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
  return ResponseUtil.ok(stoStockOutPlanDubboService.export(stoStockOutPlanVO));
 }

 @GetMapping("/lateStockOutPlan")
 @ApiOperation(value = "逾期出库计划")
 @ApiImplicitParams({
         @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
         @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
 })
 public ApiResult lateStockOutPlan(StoStockOutPlanLateVO stoStockOutPlanLateVO,Integer pageNum,Integer pageSize, @CurrentUser LoginInfo loginInfo) throws Exception {
  return ResponseUtil.ok(stoStockOutPlanDubboService.lateStockOutPlan(stoStockOutPlanLateVO,pageNum,pageSize));
 }

 @GetMapping("/planListByVin")
 @ApiOperation(value = "根据VIN码查询出库计划")
 @ApiImplicitParams({
         @ApiImplicitParam(paramType = "query", name = "vin", value = "vin", required = true, dataType = "String"),
         @ApiImplicitParam(paramType = "query", name = "rfid", value = "rfid", required = true, dataType = "String")
 })
 public ApiResult planListByVin(String vin,String rfid) throws Exception {
  return ResponseUtil.ok(stoStockOutPlanDubboService.planListByVin(vin,rfid));
 }

}
